package com.flower.service.impl;


import java.util.List;

import com.flower.entity.Catalog;
import com.flower.mapper.CatalogMapper;
import com.flower.mapper.ImagesMapper;
import com.flower.service.CatalogService;
import com.flower.utils.PageResult;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Map;

/**
 * CatalogServiceImpl 服务接口实现类
 * @date 2019-03-09 12:00:24
 * @version 1.0
 */
@Service
@Transactional
public class CatalogServiceImpl implements CatalogService {

	@Autowired
	private CatalogMapper catalogMapper;


	/** 添加方法 */
	public void save(Catalog catalog){
		try {
			catalogMapper.insertSelective(catalog);
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/** 修改方法 */
	public void update(Catalog catalog){
		try {
			catalogMapper.updateById(catalog);
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/** 根据主键id删除 */
	public void delete(Serializable id){
		try {
			catalogMapper.deleteByPrimaryKey(id);
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

    /** 批量删除 */
    public void deleteAll(Serializable[] ids){
        try {
            // 创建示范对象
            Example example = new Example(Catalog.class);
            // 创建条件对象
            Example.Criteria criteria = example.createCriteria();
            // 创建In条件
            criteria.andIn("cid", Arrays.asList(ids));
            // 根据示范对象删除
            catalogMapper.deleteByExample(example);
        }catch (Exception ex){
            throw new RuntimeException(ex);
        }
    }

	/** 根据主键id查询 */
	public Catalog findOne(Serializable cId){
		try {
			return catalogMapper.selectByPrimaryKey(cId);
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/** 查询全部 */
	public List<Map<String,Object>> findAll(){
		try {
			return catalogMapper.findAll();
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

	/** 多条件分页查询 */
	public PageResult findByPage(Catalog catalog, int page, int rows){
		try {
			PageInfo<Catalog> pageInfo = PageHelper.startPage(page, rows)
				.doSelectPageInfo(new ISelect() {
					@Override
					public void doSelect() {
						catalogMapper.selectAll();
					}
				});
			return new PageResult(pageInfo.getTotal(),pageInfo.getList());
		}catch (Exception ex){
			throw new RuntimeException(ex);
		}
	}

}